//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.sinopec.siam.agent.sp.config;

import com.sinopec.siam.agent.common.Tool;
import com.sinopec.siam.apache.log4j.Logger;
import com.sinopec.siam.opensaml.xml.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.Properties;

public class SysConfig {
    private static Logger log = Logger.getLogger(SysConfig.class);
    private static final String DES = "DES";
    public static String sp_auth_method_level_file = null;
    public static String sp_credential_keyStoreType = null;
    public static String sp_credential_keyStorePath = null;
    public static String sp_credential_keyStorePassword = null;
    public static String sp_credential_keyPassword = null;
    public static String sp_credential_keyAlias = null;
    public static String sp_auth_method = null;
    public static String sp_saml2ds_response_requestPaths = null;
    public static String siam_sp_proxy_web_urls = null;
    public static String siam_sp_proxy_load_url = null;
    public static String siam_sp_proxy_flag = null;
    public static String siam_sp_eai_junction1_websealProtocol = null;
    public static String siam_sp_eai_junction1_websealHostname = null;
    public static int siam_sp_eai_junction1_websealPort = -1;
    public static String siam_sp_eai_junction1_appServerProtocol = null;
    public static String siam_sp_eai_junction1_appServerHostname = null;
    public static int siam_reponse_safe_time_range = 0;
    public static int siam_sp_eai_junction1_appServerPort = -1;
    public static String siam_sp_eai_junction1_websealJunctionUri = null;
    public static boolean siam_sp_eai_junction1_transparentJunction = false;
    public static String siam_sp_eai_junction2_websealProtocol = null;
    public static String siam_sp_eai_junction2_websealHostname = null;
    public static int siam_sp_eai_junction2_websealPort = -1;
    public static String siam_sp_eai_junction2_appServerProtocol = null;
    public static String siam_sp_eai_junction2_appServerHostname = null;
    public static int siam_sp_eai_junction2_appServerPort = -1;
    public static String siam_sp_eai_junction2_websealJunctionUri = null;
    public static boolean siam_sp_eai_junction2_transparentJunction = false;
    public static String session_local = null;
    public static String sp_jsp_error_handler_path = null;
    public static String sp_login_tsysaccount = null;
    public static String sp_saml2_slo_sloPath = null;
    public static boolean sp_saml2_slo_redirectToLogin = false;
    public static String sp_saml2_slo_requestPaths = null;
    public static int sp_ParserPool_maxPoolSize = -1;
    public static boolean sp_ParserPool_coalescing = false;
    public static boolean sp_ParserPool_ignoreComments = false;
    public static boolean sp_ParserPool_ignoreElementContentWhitespace = false;
    public static boolean sp_ParserPool_namespaceAware = false;
    public static String sp_metadata_url = null;
    public static String sp_metadata_url_head = null;
    public static String sp_metadata_url_entityId = null;
    public static String sp_metadata_backUpPath = null;
    public static String idp_metadata_url = null;
    public static String idp_metadata_url_head = null;
    public static String idp_metadata_backUpPath = null;
    public static int ts_metadata_requestTimeout = -1;
    public static boolean ts_metadata_disregardSSLCertificate = false;
    public static boolean ts_metadata_down_auto = true;
    public static int session_redis_time = 1;
    public static int ts_metadata_minRefreshDelay = -1;
    public static long ts_metadata_maxRefreshDelay = -1L;
    public static String sp_saml2_self_LLO_I18N_path = null;
    public static String sp_saml2_self_LLO_image_path = null;
    public static String sp_saml2_discoveryService_endPoint_url = null;
    public static boolean sp_saml2_discoveryService_endPoint_isPassive = false;
    public static String sp_saml2_discoveryService_endPoint_isCache = null;
    public static String sp_saml2_idp_entityId = null;
    public static String sp_saml2_idp_themeOfIdPLoginPage = null;
    public static final String[] profileBeanNames = new String[]{"siam.sp.handler.Metadata", "siam.sp.handler.SAML2SSO.Checker", "siam.sp.handler.SAML2SSO.Response", "siam.sp.handler.SAML2SSO.SLO", "siam.sp.handler.SAML2SSO.GLO", "siam.sp.handler.SAML2SSO.LLO", "siam.sp.handler.SAML2DS.Request", "siam.sp.handler.SAML2DS.Response"};
    public static String login_users = null;

    public SysConfig() {
    }

    public static void initConfig(String propertyFilePath) throws Exception {
        Properties properties = new Properties();

        try {
            properties = Tool.getResourceProperties(propertyFilePath, properties);
            log.info("2.读取配置文件");
            sp_auth_method_level_file = getProperty("sp.auth.method.level.file", properties);
            log.info("sp_auth_method_level_file:" + sp_auth_method_level_file);
            sp_credential_keyStoreType = getProperty("sp.credential.keyStoreType", properties);
            log.info("sp_credential_keyStoreType:" + sp_credential_keyStoreType);
            sp_credential_keyStorePath = getProperty("sp.credential.keyStorePath", properties);
            log.info("sp_credential_keyStorePath:" + sp_credential_keyStorePath);

            try {
                log.info("2.1验证keystore密码");
                sp_credential_keyStorePassword = decrypt(getProperty("sp.credential.keyStorePassword", properties), "shyk2017");
                log.info("sp_credential_keyStorePassword:验证成功");
            } catch (Exception var10) {
                log.error("sp.credential.keyStorePassword is error:" + getProperty("sp.credential.keyStorePassword", properties));
                throw new Exception("sp.credential.keyStorePassword is error:" + getProperty("sp.credential.keyStorePassword", properties));
            }

            try {
                log.info("2.2验证证书密码");
                sp_credential_keyPassword = decrypt(getProperty("sp.credential.keyPassword", properties), "shyk2017");
                log.info("sp_credential_keyPassword:验证成功");
            } catch (Exception var9) {
                log.error("sp.credential.keyPassword is error:" + getProperty("sp.credential.keyPassword", properties));
                throw new Exception("sp.credential.keyPassword is error:" + getProperty("sp.credential.keyPassword", properties));
            }

            session_local = getProperty("session.local", properties);
            sp_credential_keyAlias = getProperty("sp.credential.keyAlias", properties);
            log.info("sp_credential_keyAlias:" + sp_credential_keyAlias);
            sp_auth_method = getProperty("sp.auth.method.default", properties);
            log.info("sp_auth_method:" + sp_auth_method);
            siam_sp_proxy_web_urls = getProperty("siam.sp.proxy.web.urls", properties);
            log.info("siam_sp_proxy_web_urls:" + siam_sp_proxy_web_urls);
            siam_sp_proxy_load_url = getProperty("siam.sp.proxy.load.url", properties);
            log.info("siam_sp_proxy_load_url:" + siam_sp_proxy_load_url);
            siam_sp_proxy_flag = getProperty("siam.sp.proxy.flag", properties);
            log.info("siam_sp_proxy_flag:" + siam_sp_proxy_flag);
            sp_saml2ds_response_requestPaths = getProperty("sp.saml2ds.response.requestPaths", properties);
            log.info("sp_saml2ds_response_requestPaths:" + sp_saml2ds_response_requestPaths);
            sp_login_tsysaccount = getProperty("sp.login.tsysAccount", properties);
            log.info("sp_login_tsysaccount:" + sp_login_tsysaccount);
            sp_jsp_error_handler_path = getProperty("sp.jsp.error.handler.path", properties);
            log.info("sp_jsp_error_handler_path:" + sp_jsp_error_handler_path);
            sp_saml2_slo_redirectToLogin = "true".equals(getProperty("sp.saml2.slo.redirectToLogin", properties));
            log.info("sp_saml2_slo_redirectToLogin:" + sp_saml2_slo_redirectToLogin);
            sp_saml2_slo_requestPaths = getProperty("sp.saml2.slo.requestPaths", properties);
            log.info("sp_saml2_slo_requestPaths:" + sp_saml2_slo_requestPaths);
            login_users = getProperty("white.login.users", properties);
            log.info("white_login_users:" + login_users);
            try {
                siam_reponse_safe_time_range = Integer.parseInt(getProperty("siam.reponse.safe.time.range", properties));
            } catch (NumberFormatException var8) {
                siam_reponse_safe_time_range = 300;
                log.info("siam.reponse.safe.time.range:" + siam_reponse_safe_time_range);
            }

            try {
                session_redis_time = Integer.parseInt(getProperty("session.redis.time", properties));
            } catch (NumberFormatException var7) {
                session_redis_time = 1;
                log.info("session.redis.time:" + session_redis_time);
            }

            try {
                sp_ParserPool_maxPoolSize = Integer.parseInt(getProperty("sp.ParserPool.maxPoolSize", properties));
                log.info("sp_ParserPool_maxPoolSize:" + sp_ParserPool_maxPoolSize);
            } catch (NumberFormatException var6) {
                sp_ParserPool_maxPoolSize = -1;
                log.info("sp_ParserPool_maxPoolSize:" + sp_ParserPool_maxPoolSize);
            }

            sp_ParserPool_coalescing = "true".equals(getProperty("sp.ParserPool.coalescing", properties));
            log.info("sp_ParserPool_coalescing:" + sp_ParserPool_coalescing);
            sp_ParserPool_ignoreComments = "true".equals(getProperty("sp.ParserPool.ignoreComments", properties));
            log.info("sp_ParserPool_ignoreComments:" + sp_ParserPool_ignoreComments);
            sp_ParserPool_ignoreElementContentWhitespace = "true".equals(getProperty("sp.ParserPool.ignoreElementContentWhitespace", properties));
            log.info("sp_ParserPool_ignoreElementContentWhitespace:" + sp_ParserPool_ignoreElementContentWhitespace);
            sp_ParserPool_namespaceAware = "true".equals(getProperty("sp.ParserPool.namespaceAware", properties));
            log.info("sp_ParserPool_namespaceAware:" + sp_ParserPool_namespaceAware);
            sp_metadata_url = getProperty("sp.metadata.url", properties);
            log.info("sp_metadata_url:" + sp_metadata_url);
            sp_metadata_url_head = getProperty("sp.metadata.ts.download.url", properties);
            log.info("sp_metadata_url_head:" + sp_metadata_url_head);
            sp_metadata_url_entityId = getProperty("sp.metadata.url.entityId", properties);
            log.info("sp_metadata_url_entityId:" + sp_metadata_url_entityId);
            sp_metadata_url = sp_metadata_url_head + sp_metadata_url + sp_metadata_url_entityId;
            log.info("sp_metadata_url:" + sp_metadata_url);
            sp_metadata_backUpPath = getProperty("sp.metadata.backUpPath", properties);
            String classpath;
            if (sp_metadata_backUpPath.startsWith("classpath:")) {
                classpath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
                sp_metadata_backUpPath = sp_metadata_backUpPath.replace("classpath:", classpath);
            }

            ts_metadata_down_auto = "true".equals(getProperty("ts.metadata.download.auto", properties));
            log.info("sp_metadata_backUpPath:" + sp_metadata_backUpPath);
            idp_metadata_url = getProperty("idp.metadata.url", properties);
            log.info("idp_metadata_url:" + idp_metadata_url);
            idp_metadata_url_head = getProperty("sp.metadata.ts.download.url", properties);
            log.info("idp_metadata_url_head:" + idp_metadata_url_head);
            idp_metadata_url = idp_metadata_url_head + idp_metadata_url;
            log.info("idp_metadata_url:" + idp_metadata_url);
            idp_metadata_backUpPath = getProperty("idp.metadata.backUpPath", properties);
            if (idp_metadata_backUpPath.startsWith("classpath:")) {
                classpath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
                idp_metadata_backUpPath = idp_metadata_backUpPath.replace("classpath:", classpath);
            }

            log.info("idp_metadata_backUpPath:" + idp_metadata_backUpPath);

            try {
                ts_metadata_requestTimeout = Integer.parseInt(getProperty("ts.metadata.requestTimeout", properties));
                log.info("ts_metadata_requestTimeout:" + ts_metadata_requestTimeout);
            } catch (NumberFormatException var5) {
                ts_metadata_requestTimeout = -1;
                log.info("ts_metadata_requestTimeout:" + ts_metadata_requestTimeout);
            }

            ts_metadata_disregardSSLCertificate = "true".equals(getProperty("ts.metadata.disregardSSLCertificate", properties));
            log.info("ts_metadata_disregardSSLCertificate:" + ts_metadata_disregardSSLCertificate);

            try {
                ts_metadata_minRefreshDelay = Integer.parseInt(getProperty("ts.metadata.minRefreshDelay", properties));
                log.info("ts_metadata_minRefreshDelay:" + ts_metadata_minRefreshDelay);
            } catch (NumberFormatException var4) {
                ts_metadata_minRefreshDelay = -1;
                log.info("ts_metadata_minRefreshDelay:" + ts_metadata_minRefreshDelay);
            }

            try {
                ts_metadata_maxRefreshDelay = Long.parseLong(getProperty("ts.metadata.maxRefreshDelay", properties));
                log.info("ts_metadata_maxRefreshDelay:" + ts_metadata_maxRefreshDelay);
            } catch (NumberFormatException var3) {
                ts_metadata_maxRefreshDelay = -1L;
                log.info("ts_metadata_maxRefreshDelay:" + ts_metadata_maxRefreshDelay);
            }

            sp_saml2_self_LLO_I18N_path = getProperty("sp.saml2.self.LLO.I18N.path", properties);
            log.info("sp_saml2_self_LLO_I18N_path:" + sp_saml2_self_LLO_I18N_path);
            sp_saml2_self_LLO_image_path = getProperty("sp.saml2.self.LLO.image.path", properties);
            log.info("sp_saml2_self_LLO_image_path:" + sp_saml2_self_LLO_image_path);
            sp_saml2_idp_entityId = getProperty("sp.saml2.idp.entityId", properties);
            log.info("sp_saml2_idp_entityId:" + sp_saml2_idp_entityId);
            sp_saml2_idp_themeOfIdPLoginPage = getProperty("sp.saml2.idp.themeOfIdPLoginPage", properties);
            log.info("sp_saml2_idp_themeOfIdPLoginPage:" + sp_saml2_idp_themeOfIdPLoginPage);
        } catch (FileNotFoundException var11) {
            var11.printStackTrace();
        } catch (IOException var12) {
            var12.printStackTrace();
        }

    }

    private static String getProperty(String propertyName, Properties variables) {
        String propValue = variables.getProperty(propertyName);
        return parsePropertyTokens(propValue, variables);
    }

    private static String parsePropertyTokens(String string, Properties variables) {
        String OPEN = "${";
        String CLOSE = "}";
        String newString = string;
        StringBuffer tmpBuffer = new StringBuffer();
        if (string != null && variables != null) {
            int start = string.indexOf("${");

            for(int end = string.indexOf("}"); start > -1 && end > start; end = newString.indexOf("}")) {
                String prepend = newString.substring(0, start);
                String append = newString.substring(end + "}".length());
                String propName = newString.substring(start + "${".length(), end);
                String propValue = variables.getProperty(propName);
                if (propValue == null) {
                    propValue = System.getProperty(propName);
                }

                if (propValue == null) {
                    newString = tmpBuffer.delete(0, tmpBuffer.length()).append(prepend).append(propName).append(append).toString();
                } else {
                    newString = tmpBuffer.delete(0, tmpBuffer.length()).append(prepend).append(propValue).append(append).toString();
                }

                start = newString.indexOf("${");
            }
        }

        return newString;
    }

    public static String decrypt(String data, String key) throws Exception {
        if (data == null) {
            return null;
        } else {
            byte[] bt;
            try {
                byte[] buf = Base64.decode(data);
                bt = decrypt(buf, key.getBytes());
            } catch (Exception var5) {
                throw new Exception(var5);
            }

            return new String(bt);
        }
    }

    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        SecureRandom sr = new SecureRandom();
        DESKeySpec dks = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(2, securekey, sr);
        return cipher.doFinal(data);
    }
}
